package com.example.gulimall.order.service.impl;

import com.example.gulimall.order.config.GuLiMallBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.order.dao.RefundInfoDao;
import com.example.gulimall.order.entity.RefundInfoEntity;
import com.example.gulimall.order.service.RefundInfoService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("refundInfoService")
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoDao, RefundInfoEntity> implements RefundInfoService {

    @Autowired private GuLiMallBeanFactory factory;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RefundInfoEntity> page = this.page(
                new Query<RefundInfoEntity>().getPage(params),
                new QueryWrapper<RefundInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void tranTest() {
        // 生成新事务
        tran1();
        // 制造异常
        String a = null;
        System.out.println(a.length());
    }


    public void tran1() {
        // 从自定义的 bean 操作类中获取 bean
        RefundInfoServiceImpl service = factory.getBean(this.getClass());
        service.tran2();
    }

    /**
     * 开启新的事务往数据库中插入数据
     * 数据库中结果： 3	1		123123123	1	1	1
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void tran2 () {
        RefundInfoEntity entity = new RefundInfoEntity();
        entity.setOrderReturnId(1L);
        entity.setRefundChannel(1);
        entity.setRefundContent("1");
        entity.setRefundSn("123123123");
        entity.setRefundStatus(1);
        this.baseMapper.insert(entity);
    }

}